package com.cn.intelligence.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cn.intelligence.mapper.TbItemCatMapper;
import com.cn.intelligence.mapper.TbItemParamItemMapper;
import com.cn.intelligence.mapper.TbItemParamMapper;
import com.cn.intelligence.pojo.EUDataGridResult;
import com.cn.intelligence.pojo.TaotaoResult;
import com.cn.intelligence.pojo.TbItemCat;
import com.cn.intelligence.pojo.TbItemParam;
import com.cn.intelligence.pojo.TbItemParamExample;
import com.cn.intelligence.pojo.TbItemParamItem;
import com.cn.intelligence.pojo.TbItemParamItemExample;
import com.cn.intelligence.service.ItemParamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * Created by Administrator on 2017/4/21.
 */
@Service
public class ItemParamServiceImpl  implements ItemParamService{

    @Autowired
    private TbItemParamMapper tbItemParamMapper;
    @Autowired
    private TbItemCatMapper tbItemCatMapper;
    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;
    @Override
    public EUDataGridResult getItemParamList(int page,int rows) {

        EUDataGridResult euDataGridResult = new EUDataGridResult();
        TbItemParamExample tbItemParamExample = new TbItemParamExample();

        PageHelper.startPage(page,rows);

        List<TbItemParam> tbItemParamList = tbItemParamMapper.selectByExampleWithBLOBs(tbItemParamExample);
        
        for (TbItemParam tbItemParam : tbItemParamList) {
        	TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(tbItemParam.getItemCatId());
        	tbItemParam.setItemCatName(tbItemCat.getName());
		}
        
        
        PageInfo<TbItemParam> pageInfo = new PageInfo<TbItemParam>(tbItemParamList);
        euDataGridResult.setRows(tbItemParamList);
        euDataGridResult.setTotal(pageInfo.getTotal());
        return euDataGridResult;
    }

    /**
     * c根据商品类目ID查询规格参数
     * @param catid
     * @return
     */
    @Override
    public TaotaoResult getItemParamByItemCatId(long catid) {
        TbItemParamExample tbItemParamExample = new TbItemParamExample();
        TbItemParamExample.Criteria  criteria = tbItemParamExample.createCriteria();
        criteria.andItemCatIdEqualTo(catid);
       List<TbItemParam> tbItemParamList = tbItemParamMapper.selectByExampleWithBLOBs(tbItemParamExample);
       if(tbItemParamList!=null && tbItemParamList.size()>0){
           return  TaotaoResult.ok(tbItemParamList.get(0));
       }
        return TaotaoResult.ok();
    }

    /**
     * 保存规格参数
     * @param itemParam
     * @return
     */
    @Override
    public TaotaoResult saveItemParam(TbItemParam itemParam) {
        itemParam.setCreated(new Date());
        itemParam.setUpdated(new Date());
        int result = tbItemParamMapper.insert(itemParam);
        return TaotaoResult.ok(result);
    }

	@Override
	public TaotaoResult deleteItemParam(Long ids[]) {
        TbItemParamExample tbItemParamExample = new TbItemParamExample();
        TbItemParamExample.Criteria  criteria = tbItemParamExample.createCriteria();
        criteria.andIdIn(Arrays.asList(ids));
        int delete = tbItemParamMapper.deleteByExample(tbItemParamExample);
        if(delete==ids.length){
        	return TaotaoResult.ok();
        }else{
        	return TaotaoResult.fail();
        }
	}
	/**
	 * 获取商品的规格参数的详细信息
	 */
	@Override
	public TaotaoResult getItemParamItemByItemId(Long itemId) {
		
		TbItemParamItemExample example = new TbItemParamItemExample();
		TbItemParamItemExample.Criteria criteria = example.createCriteria();
		criteria.andItemIdEqualTo(itemId);
		List<TbItemParamItem> list = tbItemParamItemMapper.selectByExampleWithBLOBs(example);
		if(list!=null && list.size()>0){
			return TaotaoResult.ok(list.get(0));
		}else{
			return TaotaoResult.fail();
		}
	}
}
